<!DOCTYPE html>
<html lang="en">
<style>
    ul.group-list li {
        white-space: nowrap;float: left;
        width: 150px; height: 25px;
        padding: 3px 5px;list-style-type: none;
        list-style-position: outside;border: 0px;margin: 0px;
    }
    input[type=checkbox], input[type=radio] {
        display: inline-block;
    }
</style>

{include file="Public/head"}

<body data-type="widgets">
<script src="/assets/js/theme.js"></script>
<div class="am-g tpl-g">

    {include file='Public:header'}
    {include file='Public:menu2'}



    <!-- 内容区域 -->
    <div class="tpl-content-wrapper" >
        <div class="row-content am-cf">
            <div class="row">
                <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
                    <div class="widget am-cf">
                        <div class="widget-head am-cf">
                            {if condition="$getRole['code'] == 0"}
                            <div class="widget-title  am-cf">角色添加</div>
                            {elseif condition="$getRole['code'] == 1"}
                            <div class="widget-title  am-cf">角色编辑</div>
                            {/if}
                        </div>

                        <form action="{:Url('addData')}" form="submit" id="roleform" method="post">
                            <input type="hidden" name="role_id" value="">
                            <table class="table table-bordered table-striped">
                                <tbody><tr>
                                    {if condition="$getRole['code'] == 1"}
                                    <input type="hidden" name="role_id" value="{$getRole['role_id']}">
                                    {/if}
                                    <th>角色名称:</th>
                                    {if condition="$getRole['code'] == 0"}
                                    <td><div class="col-xs-6"><input type="text" class="form-control" name="role_name" id="role_name" value=""></div></td>
                                    {elseif condition="$getRole['code'] == 1"}
                                    <td><div class="col-xs-6"><input type="text" class="form-control" name="role_name" id="role_name" value="{$getRole['role_name']}"></div></td>
                                    {/if}
                                    <th>角色描述:</th>
                                    {if condition="$getRole['code'] == 0"}
                                    <td><textarea rows="2" cols="50" name="role_desc"></textarea></td>
                                    {elseif condition="$getRole['code'] == 1"}
                                    <td><textarea rows="2" cols="50" name="role_desc" >{$getRole['role_desc']}</textarea></td>
                                    {/if}
                                </tr>
                                </tbody></table>
                            <h4><b>权限分配：</b><input type="checkbox" onclick="choosebox(this)">全选</h4>
                            <table class="table table-bordered table-striped dataTable">
                                <tbody>
                                {foreach name="data" key="key" item="value"}
                                <tr>
                                    <td class="title left" style="padding-right:50px;">
                                        <b style="font-size: 20px;color: #3bb4f2">{$group[$key]}：</b>
                                        <label class="right"><input type="checkbox" value="1" cka="mod-{$key}">全选</label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <ul class="group-list">
                                            {foreach name="value" key="k" item="v"}
                                            {if condition="$v['check'] == 1"}
                                            <li><label><input type="checkbox" class="checkbox" name="right[]" value="{$v.id}" ck="mod-{$key}" checked="checked">{$v.name}</label></li>
                                            {elseif condition="$v['check'] == 0"}
                                            <li><label><input type="checkbox" class="checkbox" name="right[]" value="{$v.id}" ck="mod-{$key}">{$v.name}</label></li>
                                            {/if}
                                            {/foreach}

                                        </ul>
                                    </td>
                                </tr>

                                {/foreach}

                            </table>
                            <tfoot>
                            <tr align="center">
                                <td>
                                    <a onclick="javascript:history.back(-1);" ><input class="btn btn-default" type="reset" value="返回">&nbsp;&nbsp;&nbsp;&nbsp;</a>


                                    <input class="btn btn-info" type="submit"  value="提交">
                                </td>
                            </tr>
                            </tfoot>
                        </form>


                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>

</body>
{include file='Public:script'}
{include file='Public:validation'}

<script>
    $(document).ready(function(){
        $(":checkbox[cka]").click(function(){
            var $cks = $(":checkbox[ck='"+$(this).attr("cka")+"']");
            if($(this).is(':checked')){
                $cks.each(function(){$(this).prop("checked",true);});
            }else{
                $cks.each(function(){$(this).removeAttr('checked');});
            }
        });
    });
    function choosebox(o){
        var vt = $(o).is(':checked');
        if(vt){
            $('input[type=checkbox]').prop('checked',vt);
        }else{
            $('input[type=checkbox]').removeAttr('checked');
        }
    }
    //提交
     function roleSubmit(){
         if($('#role_name').val() == '' ){
             layer.alert('角色名称不能为空', {icon: 2});
             return false;
         }
         var form=document.getElementById("roleform");
         var formData=new FormData(form);
         $.ajax({
             url:"{:Url('addData')}",
             type:'POST',
             data:formData,
             async: false,
             cache: false,
             contentType: false, //不设置内容类型
             processData: false, //不处理数据
             success:function(res){
                 console.log(res);
                 if (res.code != 200) {
                     layer.msg(res.msg, {shade: 0.5, time: 2000,icon: 2});
                     return false;
                 }
                 layer.msg(res.msg, {shade: 0.5, time: 2000, icon: 1}, function(){
                     window.location.href=document.referrer;

                 });
             }
         });
         // $.post("{:Url('addData')}", formData, function (res) {
             // if (res.code != 200) {
             //     layer.msg(res.msg, {shade: 0.5, time: 1000,icon: 2});
             //     return false;
             // }
             // layer.msg(res.msg, {shade: 0.5, time: 1000, icon: 1}, function () {
             //     // location.reload();
             //     location.href = "{:url('index')}";
             // });
         // });
        // $('#roleform').submit();
    }
</script>

</html>